import scrapy
from scrapy import Request
import mysql.connector
from scrapyspider.items import Contest_Continue

class contest_detail_img_urlSpider(scrapy.Spider):
    name = "contest_detail_img_url"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }

    def start_requests(self):
        conn = mysql.connector.connect(host='120.79.172.200', password='icontest', database='icontest', user='icontest')
        cursor = conn.cursor()
        cursor.execute("select url from urls")
        for i in cursor:
            url = "".join(i)
            yield Request(url, headers=self.headers)
        conn.commit()
        cursor.close()
        conn.close()

    def parse(self, response):
        item = Contest_Continue()
        contest_detail = response.xpath('//div[@class="event4-1-detail-text-box text-body clearfix"]//p').extract()
        contest_img_url = response.xpath('//img[@class="sk-event4-1-detail-img"]/@src').extract()
        contest_name = response.xpath('//h1[@class="event-title"]/text()').extract()
        item['contest_detail'] = "".join(contest_detail)
        item['contest_img_url'] = "".join(contest_img_url)
        item['contest_name'] = "".join(contest_name)
        yield item